/**
 * @file ymodem.h
 * @author QuanShen (quan.shen@prime-semi.com)
 * @brief 
 * @version 0.1
 * @date 2025-07-29
 * 
 * @copyright Copyright (c) 2025
 * 
 */
#ifndef _YMODEM_H_
#define _YMODEM_H_

#include <stdint.h>

#define PACKET_SEQNO_INDEX		(1)			// Packet 序列索引
#define PACKET_SEQNO_COMP_INDEX	(2)			// Packet 序列索引补码

#define PACKET_HEADER			(3)			// Packet 头部
#define PACKET_TRAILER			(2)			// Packet 尾部
#define PACKET_OVERHEAD			(PACKET_HEADER + PACKET_TRAILER)	// Packet 总开销
#define PACKET_SIZE				(128)		// 128 Byte Packet 数据大小
#define PACKET_1K_SIZE			(1024)		// 1K Byte Packet 数据大小

#define FILE_NAME_LENGTH		(256)		// 文件名最大长度
#define FILE_SIZE_LENGTH		(16)		// 文件大小最大长度

#define SOH						(0x01)		// Start of 128 Byte data packet
#define STX						(0x02)		// Start of 1K Byte data packet
#define EOT						(0x04)		// End of Transmission
#define ACK						(0x06)		// Acknowledge
#define NAK						(0x15)		// Negative Acknowledge
#define CAN						(0x18)		// Cancel
#define CRC16					(0x43)		// 'C'== 0x43, request 16-bit CRC

#define ABORT1					(0x41)		// 'A'== 0x41, Abort 1
#define ABORT2					(0x61)		// 'a'== 0x61, Abort 2

#define NAK_TIMEOUT				(0x100000)	// NAK 超时
#define MAX_ERRORS				(5)			// 最大错误次数

#define PACKET_RECEIVED			(0)			// Packet 接收成功
#define TIMEOUT_OR_PACKET_ERROR	(-1)		// 超时或 Packet 错误
#define ABORT_BY_USER			(1)			// 用户中止

extern uint8_t file_name[FILE_NAME_LENGTH];    // 文件名

int32_t Ymodem_Receive(uint8_t *data);
uint8_t Ymodem_Transmit(uint8_t *buff, const uint8_t* send_File_name, uint16_t f_size);

#endif
